package code.remote.request {

import code.Model;
import code.vo.Category;

import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
	

public class FetchCategoriesRequest extends AbstractRequest {
	
	public function FetchCategoriesRequest() {
		super("fetch_categories");
	}
	
	protected override function onResult(event: ResultEvent): void {
		const categories: Object = event.result.Categories;
		
		// TODO duplicate code
		const ncategories: Object = categories.NeededCategories;
		if(ncategories.Category is ArrayCollection) {
			for each(var category: Object in ncategories.Category) {
				Model.instance.neededCategories.addItem(Category.newByXml(category));
			}
		} else {
			Model.instance.neededCategories.addItem(Category.newByXml(ncategories.Category));
		}
		
		const ocategories: Object = categories.OfferedCategories;
		if(ocategories.Category is ArrayCollection) {
			for each(var ocategory: Object in ocategories.Category) {
				Model.instance.offeredCategories.addItem(Category.newByXml(ocategory));
			}
		} else {
			Model.instance.offeredCategories.addItem(Category.newByXml(ocategories.Category));
		}
		
	}
	
}
}